package mnc.game.tifo;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ComPenData extends SQLiteOpenHelper {
	
	private static final String DATABASE_TABLE = "tb_com_pan";
	private static final int DATABASE_VERSION = 1;

	private static final String TAG = "tifo";
		
	private SQLiteOpenHelper mOpenHelper;
	
	private static final String DB_NAME = "tifo_com_pan.db";
	
	/**
	 * Constructor Keeps a reference to the passed context in order to access
	 * the application's assets.
	 * 
	 * @param context
	 *            Context to be used
	 */
	public ComPenData(Context context) {
		super(context, DB_NAME, null, DATABASE_VERSION);
		System.out.println("xxxxxxxxxxxxx");
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
        try {
          System.out.println("create ... " + DATABASE_TABLE);
          db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
	                    "_id INTEGER PRIMARY KEY autoincrement, " +
	                    "time          VARCHAR,  " +
	                    "user_id       VARCHAR); ");
       } catch(Exception e) {
           System.out.println("e ... " + e);
         db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);	
         onCreate(db);  	
       }
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(TAG,
                "Upgrading " + DATABASE_TABLE + " database from version " +
                 oldVersion + " to " + newVersion +
                 ", which will destroy all old data");
        
         if(oldVersion != newVersion) {
             db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);	
             onCreate(db);  
         }
	}

	public String getPreData(Context context, String date) {

		    String ret = "NO";
		   	mOpenHelper = new ComPenData(context);
		    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		    	
			Cursor c = db.query( true, DATABASE_TABLE, new String[] {"time, user_id"}, " time < '" + date +"'", null, null, null, null, null);

			try {
				int dateInx   		  = c.getColumnIndex("time");
				int idInx             = c.getColumnIndex("user_id");
			
				if(c.moveToFirst()) {
					System.out.println("전날 delete");
					String daleteMe = "DELETE FROM " + DATABASE_TABLE +  " WHERE time = '" + date + "'";
		            db.execSQL(daleteMe);
				}	
			} finally {
				if (c != null) c.close();
				if (db != null && db.isOpen()) {
					db.close();
				}
			}
			
			return ret;
	}
	
	public ArrayList<String> getDataList(Context context, String date) {

	   	mOpenHelper = new ComPenData(context);
	    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		    	
		Cursor c = db.query( true, DATABASE_TABLE, new String[] {"time, user_id"}, " time = '" + date + "'", null, null, null, null, null);
		ArrayList<String> ret = new ArrayList<String>();
			
		try {
				
			int dateInx   		  = c.getColumnIndex("time");
			int idInx             = c.getColumnIndex("user_id");
			
			if (c.moveToFirst()) {
				do {
					String tmp = c.getString(idInx);
					System.out.println("tmp = " + tmp);
					ret.add(tmp);
				} while (c.moveToNext());
			}
		} finally {
			if (c != null) c.close();
			if (db != null && db.isOpen()) {
				db.close();
			}
		}
			
		return ret;
   }
   public String getData(Context context, String date,  String id) {

	    String ret = "NO";
	   	mOpenHelper = new ComPenData(context);
	    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
	    	
		Cursor c = db.query( true, DATABASE_TABLE, new String[] {"time, user_id"}, " time = '" + date +"' AND user_id = '" + id + "'", null, null, null, null, null);

		try {
			
			int dateInx   		  = c.getColumnIndex("time");
			int idInx             = c.getColumnIndex("user_id");
		
			if (c.moveToFirst()) {
				System.out.println("오늘 date가 있습니다 "+ c.getString(idInx));
				ret = "OK";
			} else {
				System.out.println("오늘 date가 없습니다. insert ");
                String insertMe = "INSERT INTO " + DATABASE_TABLE +  "(time, user_id) " +  " VALUES ";
                insertMe = insertMe + "('"+ date +"','"+ id + "');";
                db.execSQL(insertMe);
			}
		} finally {
			if (c != null) c.close();
			if (db != null && db.isOpen()) {
				db.close();
			}
		}
		
		return ret;
   }
   
   public void prcs(Context context, String type, String date, String id) throws Exception {
	   
	   mOpenHelper = new ComPenData(context);
       SQLiteDatabase db = mOpenHelper.getReadableDatabase();
       
       try {
           if(type.equals("I")) {
                String insertMe = "INSERT INTO " + DATABASE_TABLE +  "(time, user_id) " +  "VALUES ";
                insertMe = insertMe + "('"+ date +"','"+ id + "');";
                db.execSQL(insertMe);
           } else if(type.equals("D")) {
               String daleteMe = "DELETE FROM " + DATABASE_TABLE +  " WHERE time = '" + date + "'";
               db.execSQL(daleteMe);
           }
        } catch(Exception e) {
        	throw e;
        } finally {
        	try {
				if (db != null && db.isOpen()) {
					db.close();
				}
            } catch(Exception e) {
            	throw e;
            }
        }
    }    
}
